﻿using System;

namespace SilverChart.Core.Configuration {
    public abstract class AbstractValueItemHelper<T> : ValueItemHelper<T>{
        private Boolean isValueAssigned = false;
        private T value;
        private ConfigurationItem parentItem;
        private String itemName;

        public AbstractValueItemHelper(ConfigurationItem parentItem, String itemName) {
            this.parentItem = parentItem;
            this.itemName = itemName;
        }

        public T Value {
            get {
                if (!isValueAssigned) {
                    if (!parentItem.TryGetConfigurationValue<T>(itemName, out value)) {
                        value = GetDefaultValue();
                    }
                    isValueAssigned = true;
                }
                return value;
            }
        }

        protected abstract T GetDefaultValue();
    }
}
